package app.server.project.main.service.db.entity.user;

import app.server.project.core.constants.Def;
import app.server.project.core.syndb.SynDBMgr;
import app.server.project.core.syndb.SynSpeed;
import app.server.project.core.util.SysTimeUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.time.LocalDateTime;

@Data
@Entity(name = "user_sign")
@TableName("user_sign")
@Comment(value = "用户签到表")
@SynSpeed
public class UserSignEntity {

    @Id
    @Column(name = "id")
    @Comment("用户主键")
    @TableId("id")
    private long id;

    @TableField("sign_time")
    @Comment("最后登录时间")
    @Column(name = "sign_time")
    private LocalDateTime signTime;

    @TableField("user_id")
    @Comment("玩家id")
    @Column(name = "user_id")
    private long userId;

    @TableField("sign_type")
    @Comment("签到类型")
    @Column(name = "sign_type")
    private int signType;

    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    public static UserSignEntity of(long userId, long signId) {
        var ret = new UserSignEntity();
        ret.setId(signId);
        ret.setSynDb(true);
        ret.setUserId(userId);
        ret.setSignType(Def.Zero);
        ret.setSignTime(SysTimeUtil.getAppTimeLocalTime());
        return ret;
    }

    public void setSignType(int signType) {
        this.signType = signType;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserSignEntity::getSignType, this);
    }

    public void setSignTime(LocalDateTime signTime) {
        this.signTime = signTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserSignEntity::getSignTime, this);
    }

    public void setUserId(long userId) {
        this.userId = userId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserSignEntity::getUserId, this);
    }

}
